//This class can write classes, individuals, properties in a given ontology

package it.uniroma2.gestoreontologie;

import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.impl.StatementImpl;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.ontology.*;

public class Gestore implements Serializable{

	private String ns;
	private String uri;

	public Gestore(String ns, String uri) {

		this.uri = uri;
		this.ns = ns;
	}

	public OntModel leggiOnt() {

		OntModel base = ModelFactory
				.createOntologyModel(ProfileRegistry.OWL_LANG);
		base.read(uri, "RDF/XML-ABBREV");
		for (Iterator i = base.listClasses(); i.hasNext();) {
			OntClass c = (OntClass) i.next();
		}

		return base;
	}

	public void creaClasse(String classe, String sottoClasse) {
		OntModel base = leggiOnt();
		OntClass prova = base.createClass(ns + "#" + sottoClasse);
		for (Iterator i = base.listClasses(); i.hasNext();) {
			OntClass c = (OntClass) i.next();
			System.out.println("Class " + c.getURI());
			if (c.getURI().compareTo(ns + "#" + classe) == 0) {
				c.addSubClass(prova);
				System.out.println("Aggiunto");
				break;
			}

		}
		this.salvaOnt(base);

	}

	public void aggiungiIndividual(String ind, String classe) {
		OntModel base = leggiOnt();
		for (Iterator i = base.listClasses(); i.hasNext();) {
			OntClass c = (OntClass) i.next();
			if (c.getURI().compareTo(ns + "#" + classe) == 0) {
				Individual individual = c.createIndividual(ns + "#" + ind);
				System.out.println("Aggiunto ind");
				break;
			}

		}
		this.salvaOnt(base);
	}
	
	
	public void insertProp(String istanza, String prop){
		OntModel base = leggiOnt();
		Individual ind = base.getIndividual(ns + "#" +istanza);
		ObjectProperty op = base.getObjectProperty(ns + "#" + prop);
		Individual ind2 = base.getIndividual(ns + "#" + "XDG");
		ind.setPropertyValue(op, ind2);
		
		this.salvaOnt(base);
	}
	
	public void insertProp2(String istanza, String prop, String subistanza){
		OntModel base = leggiOnt();
		Individual ind = base.getIndividual(ns + "#" +istanza);
		ObjectProperty op = base.getObjectProperty(ns + "#" + prop);
		Individual ind2 = base.getIndividual(ns + "#" + subistanza);
		//ind.setPropertyValue(op, ind2);
		ind.addProperty(op, ind2);
		this.salvaOnt(base);
	}
	

	public Vector<String> cercaIndividual(String classe) {
		Vector<String> vettoreIndividual = new Vector<String>();
		OntModel base = leggiOnt();
		for (Iterator i = base.listClasses(); i.hasNext();) {
			OntClass c = (OntClass) i.next();
			if (c.getURI() != null && c.getURI().compareToIgnoreCase(ns + "#" + classe) == 0) {
				for (Iterator i2 = c.listInstances(); i2.hasNext();) {
					Individual indi = (Individual) i2.next();
					vettoreIndividual.addElement(indi.getURI());
				}
				break;
			}

		}
		return vettoreIndividual;
	}

	public Vector<String> recuperaIstanze() {
		Vector<String> vettoreIndividual = new Vector<String>();
		OntModel base = leggiOnt();
		
		for (Iterator i = base.listClasses(); i.hasNext();) {
			OntClass c = (OntClass) i.next();
			for (Iterator i2 = c.listInstances(); i2.hasNext();) {
				Individual indi = (Individual) i2.next();
				vettoreIndividual.addElement(eliminaNs(indi.getURI()));
			}
		}
		return vettoreIndividual;
	}
	
	public String recuperaProprieta(String istanza, String prop){
		OntModel base = leggiOnt();
		Individual ind = base.getIndividual(ns + "#" +istanza);
		ObjectProperty op = base.getObjectProperty(ns + "#" + prop);
		if(ind.getPropertyValue(op) == null){
			return "*";
		}
		return eliminaNs(ind.getPropertyValue(op).toString());
	}
	public Vector<String> recuperaProprietaVect(String istanza, String prop){
		Vector<String> proVect = new Vector();
		OntModel base = leggiOnt();
		Individual ind = base.getIndividual(ns + "#" +istanza);
		ObjectProperty op = base.getObjectProperty(ns + "#" + prop);
		for(Iterator i = ind.listPropertyValues(op); i.hasNext();){
			proVect.add(eliminaNs(i.next().toString()));
		}
		return proVect;
		
	}
	
	public String recuperaDataPro(String istanza, String prop){
		OntModel base = leggiOnt();
		Individual ind = base.getIndividual(ns + "#" +istanza);
		DatatypeProperty dp = base.getDatatypeProperty(ns + "#" + prop);
		if(ind.getPropertyValue(dp) == null){
			return "*";
		}
		return eliminaNs(ind.getPropertyValue(dp).toString());
	}

	public void salvaOnt(OntModel model) {
		try {
			FileOutputStream fout = new FileOutputStream("prova.owl");
			model.write(fout, "RDF/XML-ABBREV");
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}
	
	public String eliminaNs(String ns){
		if(ns!=null){
			int index = ns.indexOf("#");
			return ns.substring(index + 1);
		} else return null;
		
	}

}
